#include <gtest/gtest.h>

#include "common_components/log/log_base.h"
#include "common_components/log/log.h"
#include "generated/base_options.h"
#include "libpandabase/utils/logger.h"

using namespace panda;

// 测试 ConvertFromRuntime
TEST(LogTest, ConvertFromRuntime_Info_ReturnsInfo) {
    EXPECT_EQ(common::Log::ConvertFromRuntime(common::LOG_LEVEL::INFO), Level::INFO);
}

TEST(LogTest, ConvertFromRuntime_Warn_ReturnsWarn) {
    EXPECT_EQ(common::Log::ConvertFromRuntime(common::LOG_LEVEL::WARN), Level::WARN);
}

TEST(LogTest, ConvertFromRuntime_Error_ReturnsError) {
    EXPECT_EQ(common::Log::ConvertFromRuntime(common::LOG_LEVEL::ERROR), Level::ERROR);
}

TEST(LogTest, ConvertFromRuntime_Fatal_ReturnsFatal) {
    EXPECT_EQ(common::Log::ConvertFromRuntime(common::LOG_LEVEL::FATAL), Level::FATAL);
}

TEST(LogTest, ConvertFromRuntime_Debug_ReturnsDebug) {
    EXPECT_EQ(common::Log::ConvertFromRuntime(common::LOG_LEVEL::DEBUG), Level::DEBUG);
}

#ifdef ENABLE_HILOG
namespace {
LogLevel test_hi_log_level = LogLevel::LOG_DEBUG;
bool test_hilog_is_loggable = true;

LogLevel GetHiLogLevel() {
    return test_hi_log_level;
}

Level ConvertToLevel(LogLevel hilogLevel) {
    switch (hilogLevel) {
        case LogLevel::LOG_INFO: return Level::INFO;
        case LogLevel::LOG_WARN: return Level::WARN;
        case LogLevel::LOG_ERROR: return Level::ERROR;
        case LogLevel::LOG_FATAL:
        case LogLevel::LOG_LEVEL_MAX: return Level::FATAL;
        case LogLevel::LOG_DEBUG:
        default: return Level::DEBUG;
    }
}
} // namespace

TEST(LogTest, ConvertFromRuntime_Follow_WithHiLogEnabled_ReturnsConvertedLevel) {
    test_hi_log_level = LogLevel::LOG_WARN;
    EXPECT_EQ(common::Log::ConvertFromRuntime(LOG_LEVEL::FOLLOW), Level::WARN);
}
#else
// TEST(LogTest, ConvertFromRuntime_Follow_WithoutHiLog_DefaultsToDebugOrError) {
//     EXPECT_TRUE(common::Log::ConvertFromRuntime(common::LOG_LEVEL::FOLLOW) == Level::DEBUG ||
//                 common::Log::ConvertFromRuntime(common::LOG_LEVEL::FOLLOW) == Level::ERROR);
// }
#endif

// 测试 LevelToString
TEST(LogTest, LevelToString_Info_ReturnsStringInfo) {
    EXPECT_EQ(common::Log::LevelToString(Level::INFO), "info");
}

TEST(LogTest, LevelToString_Warn_ReturnsStringWarning) {
    EXPECT_EQ(common::Log::LevelToString(Level::WARN), "warning");
}

TEST(LogTest, LevelToString_Error_ReturnsStringError) {
    EXPECT_EQ(common::Log::LevelToString(Level::ERROR), "error");
}

TEST(LogTest, LevelToString_Fatal_ReturnsStringFatal) {
    EXPECT_EQ(common::Log::LevelToString(Level::FATAL), "fatal");
}

TEST(LogTest, LevelToString_Debug_ReturnsStringDebug) {
    EXPECT_EQ(common::Log::LevelToString(Level::DEBUG), "debug");
}

// 测试 Initialize
TEST(LogTest, Initialize_OptionsSet_CorrectlyInitialized) {
    common::LogOptions options;
    options.level = Level::INFO;
    options.component = static_cast<ComponentMark>(Component::GC);

    common::Log::Initialize(options);

    EXPECT_EQ(common::Log::GetLogLevel(), Level::INFO);
    EXPECT_EQ(common::Log::GetLogComponents(), static_cast<ComponentMark>(Component::GC));
}